package com.gy.ui;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.ColorRes;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import com.gy.R;

/**
 * author：created by gaoyuan on 2019-09-06
 * description：
 */
public class LoadingProgressView extends View {
    
    private Paint mPaint;
    private Path mPath;
    private int mWidth;
    private int mHeight;
    private float mAnimatedValue;
    private Rect mSrc;
    private Bitmap mBitmap;
    private RectF mDst;
    private RectF mOval;
    private RectF mRect;
    private RectF mOval1;

    public LoadingProgressView(Context context) {
        super(context);
        init(null, 0);
    }

    public LoadingProgressView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0);
    }

    public LoadingProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs, defStyleAttr);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth =w;
        mHeight=h;
        mOval = new RectF(0, 0, mHeight, mHeight);
        mRect = new RectF(mHeight / 2f, 0, mWidth - mHeight / 2f, mHeight);
        mOval1 = new RectF(mWidth - mHeight, 0, mWidth, mHeight);
        mBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.fengshan);
        mSrc = new Rect(0,0, mBitmap.getWidth(), mBitmap.getHeight());
        float offset = 16;
        mDst = new RectF(mWidth-mHeight+offset,offset,mWidth-offset,mHeight-offset);
        mPath.addArc(mOval,90,180);
        mPath.addRect(mRect, Path.Direction.CW);
        mPath.addArc(mOval1,-90,180);
        mPath.addCircle(mWidth-mHeight/2f,mHeight/2f,mHeight/2f, Path.Direction.CW);
    }

    private void init(AttributeSet attrs, int defStyle) {
        // Load attributes
        mPaint = new Paint();
        mPaint.setColor(getColor(R.color.lightYellow));
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeWidth(8f);
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPath = new Path();
    }

    @Override
    protected void onDraw(final Canvas canvas) {
        super.onDraw(canvas);
        //背景
        mPaint.setColor(getColor(R.color.lightYellow));
        mPaint.setStyle(Paint.Style.FILL);

        canvas.drawPath(mPath, mPaint);
        
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.WHITE);
        canvas.drawCircle(mWidth-mHeight/2f,mHeight/2f,mHeight/2f-8, mPaint);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(getColor(R.color.yellow));
        canvas.drawCircle(mWidth-mHeight/2f,mHeight/2f,mHeight/2f-8, mPaint);
        //draw 风扇
        canvas.save();
        canvas.rotate(mAnimatedValue *360f,mWidth-mHeight/2f,mHeight/2f);
        //Log.d("LoadingProgressView", "animatedValue: "+ mAnimatedValue*360);
        canvas.drawBitmap(mBitmap, mSrc, mDst,null);
        canvas.restore();

        mPath.close();
        
    }
    
    public void startFan(){
        ValueAnimator animator = ValueAnimator.ofFloat(0f,1.0f);
        animator.setInterpolator(new AccelerateDecelerateInterpolator());
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.setDuration(1000);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                //invalidate
                //百分比
                mAnimatedValue = (float) animation.getAnimatedValue();
                Log.d("LoadingProgressView", "animatedValue: "+ mAnimatedValue*360);
                invalidate();
                //draw 风扇
            }
        });
        animator.start();
    }

    private int getColor(@ColorRes int id) {
        return getResources().getColor(id);
    }
}
